package com.crm.CLdriving.service.impl;


import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.crm.CLdriving.common.BaseResponse;
import com.crm.CLdriving.common.PageResponse;
import com.crm.CLdriving.dto.PageReq.PageNumber;
import com.crm.CLdriving.dto.resp.CoachSelectAllRespDto;
import com.crm.CLdriving.enu.ResponseEnum;
import com.crm.CLdriving.mapper.NewsMapper;
import com.crm.CLdriving.po.CoachPO;
import com.crm.CLdriving.vo.NewsCondition;
import com.crm.CLdriving.po.NewsPO;
import com.crm.CLdriving.service.NewsService;
import lombok.AllArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@AllArgsConstructor
@Log4j2
public class NewsServiceImpl extends ServiceImpl<NewsMapper,NewsPO> implements NewsService {

    private final NewsMapper newsMapper;

    /**
     * 查询全部新闻
     * @param pageNumber
     * @return
     */
    @Override
    public PageResponse<List<NewsPO>> selectAll(PageNumber pageNumber) {
        List<NewsPO> response = new ArrayList<NewsPO>();
        Page<NewsPO> page = new Page(pageNumber.getPageNumber(),10);
        List<NewsPO> list = newsMapper.selectPage(page,null);
        if (!"[]".equals(list.toString())) {
            log.info("查询全部新闻为："+list.toString());
            try {
                for (NewsPO newsPO : list) {
                    response.add(newsPO);
                }
                log.info("copy后："+response.toString());
            } catch (Exception e) {
                log.info("copy错误："+e);
            }
        }
        return PageResponse.successOfPage(response,page);
    }

    @Override
    public List<NewsPO> findByCondition(Page<NewsPO> page, NewsCondition vo) {
        List<NewsPO> list = newsMapper.findByCondition(page, vo);
        if (CollUtil.isNotEmpty(list)) {
            List<Integer> ids = new ArrayList<>();
            for (NewsPO news : list) {
                ids.add(news.getId());
            }
        }
        return list;
    }

    @Override
    @Cacheable(value = "article", key = "'slider'")
    public List<NewsPO> sliderList() {
        NewsCondition vo = new NewsCondition();
        vo.setSlider(true);
//        vo.setStatus(CoreConst.STATUS_VALID);
        return this.findByCondition(null, vo);
    }

    @Override
    @Cacheable(value = "article", key = "'recommended'")
    public List<NewsPO> recommendedList(int pageSize) {
        NewsCondition vo = new NewsCondition();
        vo.setRecommended(true);
//        vo.setStatus(CoreConst.STATUS_VALID);
        vo.setPageSize(pageSize);
        Page<NewsPO> page = new Page<>(vo.getPageNumber(), vo.getPageSize());
        return this.findByCondition(page, vo);
    }

    @Override
    @Cacheable(value = "article", key = "'recent'")
    public List<NewsPO> recentList(int pageSize) {
        NewsCondition vo = new NewsCondition();
        vo.setPageSize(pageSize);
//        vo.setStatus(CoreConst.STATUS_VALID);
        vo.setRecentFlag(true);
        Page<NewsPO> page = new Page(vo.getPageNumber(), vo.getPageSize());
        return this.findByCondition(page, vo);
    }

    @Override
    @Cacheable(value = "article", key = "'random'")
    public List<NewsPO> randomList(int pageSize) {
        NewsCondition vo = new NewsCondition();
        vo.setRandom(true);
//        vo.setStatus(CoreConst.STATUS_VALID);
        vo.setPageSize(pageSize);
        Page<NewsPO> page = new Page(vo.getPageNumber(), vo.getPageSize());
        return this.findByCondition(page, vo);
    }

    @Override
    @Cacheable(value = "article", key = "'hot'")
    public List<NewsPO> hotList(int pageSize) {
        Page<NewsPO> page = new Page(1, pageSize);
        return newsMapper.hotList(page);
    }


    @Override
    @Cacheable(value = "article", key = "#id")
    public NewsPO selectById(Integer id) {
        return newsMapper.getById(id);
    }

    @Override
    @CacheEvict(value = "article", allEntries = true)
    public BaseResponse<?> insertArticle(NewsPO newsPO) {

//        NewsPO newsPO = new NewsPO();
        try {
//            BeanUtils.copyProperties(newsPO, newsInsertReqDto);
            Date date = new Date();
            newsPO.setCreateTime(date);
            newsPO.setUpdateTime(date);
            newsPO.setAuthor("管理员");
            log.info("copy后："+ newsPO);
        } catch (Exception e) {
            log.info("copy:"+e);
        }
        Integer row = newsMapper.insert(newsPO);
        if (row == 1) {
            log.info("row____"+row);
            return BaseResponse.message(ResponseEnum.LRCG);
        }
        return BaseResponse.message(ResponseEnum.FAIL);
    }

    @Override
    @CacheEvict(value = "article", allEntries = true)
    public int deleteBatch(Integer[] ids) {
        int count = 0;
        for(Integer id:ids){
            count += newsMapper.deleteById(id);
        }
        return count;
    }


}
